/**
 * @author Cheng Liu
 **/

#include "stdafx.h"
#include "PluginFacade.h"
#include "../mfc/CineApp.h"
#include "../Event/EventBus.h"
#include "../mfc/MainFrm.h"
#include "../TextBuffer/TextBufferView.h"
#include "../TextBuffer/TextBuffer.h"
#include "../Accelerator/Accelerator.h"

using namespace Events;

IMPLEMENT_SINGLETON(PluginFacade)

PluginFacade::PluginFacade() {
	WriteLog("PluginFacade Created\n");
	eventBus = EventBus::Instance();
	menuManager = NULL;
}


void PluginFacade::AttachListener( const EventType& type, IEventHandler* handler ) {
	eventBus->AttachListener(type, handler);
}

bool PluginFacade::DettachListener( const EventType& type, IEventHandler* handler )
{
	return eventBus->DettachListener(type, handler);
}

void PluginFacade::ReleaseListener( const IEventHandler* handler )
{
	eventBus->ReleaseListener(handler);
}

void PluginFacade::PublishEvent(Event * event )
{
	eventBus->PublishEvent(event);
}

UINT PluginFacade::RegisterCommand( IEventHandler* eventHandler ) {
	return menuManager->RegisterCommand(eventHandler);
}

void PluginFacade::ReleaseCommand( UINT nCmd ) {
	menuManager->ReleaseCommand(nCmd);
}

BVHND PluginFacade::GetActiveTextView() {
	CWnd* active = mainFrm->GetActiveView();
	if (active==NULL)
		return NULL;

	if (active->IsKindOf(RUNTIME_CLASS(CTextBufferView)))
		return (BVHND)active;
	return NULL;
}

std::vector<BHND > PluginFacade::GetTextBufferList() {
	return BufferController::Instance()->GetTextBufferList();
}

std::basic_string<TCHAR> PluginFacade::GetTextBufferName( BHND bhnd ) {
	TextBuffer* buffer = BufferController::Instance()->GetTextBufferByHandle(bhnd);
	CFile file;
	file.SetFilePath(buffer->PathFilename());
	return file.GetFileName().GetString();
}

void PluginFacade::SetTextView( BVHND textView , BHND bhnd ) {
	ASSERT(((CWnd*)textView)->IsKindOf(RUNTIME_CLASS(CTextBufferView)));
	CTextBufferView* view = (CTextBufferView*)textView;
	view->ReAttachBuffer((BufferBase*)BufferController::Instance()->GetTextBufferByHandle(bhnd));
}

void PluginFacade::SetFocus( BVHND view ) {
	ASSERT(::IsWindow(((CWnd*)view)->GetSafeHwnd()));
	((CWnd*)view)->SetFocus();
	CWnd x;
}

BHND PluginFacade::OpenTextBuffer( std::basic_string<TCHAR> pathFileName ) {
	BufferCreateInfo info;
	memset(&info,0,sizeof(info));
	info.createInfo = (UINT_PTR)pathFileName.c_str();
	return (BHND)BufferController::Instance()->CreateTextBuffer(&info);
}

BOOL PluginFacade::RegisterAccelKey( char* filePath, char* str, int cmd ) {
	Accelerator* accel = ((CCineApp*)AfxGetApp())->GetAccel();
	return LoadAccelConfig(filePath,accel,str,cmd);
}
// void PluginFacade::SetColor( BVHND view, UINT color, int start, int length ) {
// 	ASSERT(::IsWindow(((CWnd*)view)->GetSafeHwnd()));
// 	ASSERT(((CWnd*)view)->IsKindOf(RUNTIME_CLASS(CCodeEdit)));
// 	CCodeEdit* edit = (CCodeEdit*)view;
// 	edit->StartStyling(start,0x1f);
// 	edit->SetStyling(length,color);
// }